/*option = {
 URL: URL + '/account/login/t/wechat',
 val: {
 email: '[name="email"]',
 pwd: '[name="password"]',
 openid: $('[name="openid"]').val()
 },
 defaultVal: {
 type: $_GET['type'] || 'C'
 }
 }*/
function simpleForm(selector, option, trigger, onSubmint, onValid, onInValid, callback) {

    // if (option.captcha) {
    //     var gt = new GT(trigger);
    //     //gt.geeTestInit();
    // }
    //
    // var cb = function(res) {
    //     gt.refresh();
    //     callback(res);
    // };
    //
    // var doPostCaptcha = function(gt) {
    //     var data = _makeData(option.val, option.defaultVal);
    //     var vaildate = gt.getValidate();
    //     for (var i in vaildate) {
    //         data[i] = vaildate[i];
    //     }
    //     // console.log(JSON.stringify(data));
    //     $.post(option.URL, data, cb, 'json').error(function() {
    //         gt.refresh();
    //         toast('操作失败');
    //     });
    // };

    var doPost = function() {
        var data = _makeData(option.val,option._xsrf);
        $.post(option.URL, data, callback, 'json');
    };

    $(selector).on('click', trigger, function() {
        var result = $(selector).validator('isFormValid');
        onSubmint(result);
        doPost();
    });


    $(selector).validator({
        activeClass: '',

        // 验证不通过时添加到域上的 class
        inValidClass: 'form-error',

        // 验证通过时添加到域上的 class
        validClass: '',
        onValid: function() {

        },
        onInValid: function(validity) {
            var $field = $(validity.field);
            var $group = $field.closest('.input-control');
            $field.addClass('form-error');

            var msg = $field.attr('data-validation-message') || this.getValidationMessage(validity);
            onInValid($group, msg);
        }
    });

    function _makeData(val,_xsrf) {
        var postData = {};
        var data = {};

        postData['_xsrf'] = $(_xsrf).val();
        for (var param in val) {
            void 0;
            if ($(val[param]).length && !!$(val[param]).val()) {
                data[param] = $(val[param]).val();
            }
        }
        postData['postdata'] = data;
        return postData;
    }
}

function clearForm(input) {
    $(input).on('keyup', function() {
        if ($(this).val().length > 0) {
            $(this).parent('.input-control').next('.J_Clear').show();
        } else {
            $(this).parent('.input-control').next('.J_Clear').hide();
        }
    });
    $('.form-group').on('click', '.J_Clear', function() {
        $(this).prev('.input-control').children(input).val('');
        $(this).prev('.input-control').children(input).focus();
        $(this).hide();
    });
}
